// 处理数据逻辑的同步执行的方法的集合，Vuex中store数据改变的唯一方法commit mutations
// mutations.js文件中做对deptTitle改变的操作

/**
 * 处理权限信息
 * @param permissions
 * @returns {{permissionRs: [], moduleNames: []}}
 */
const handelPer = (permissionObj) => {
  const permissionRs = []

  const inputArray = (array ,item) => {
    if (!array.includes(item)) {
      array.push(item)
    }
  }
  const keys = Object.keys(permissionObj)
  keys.forEach((key) => {
    const permissions = permissionObj[key]
    permissions.forEach((item) => {
      let word = item.replaceAll(':','-')
      const wordsArray = word.split('-')
      const len = wordsArray.length < 3 ? wordsArray.length : 3
      switch (len) {
        case 1:
          inputArray(permissionRs, word)
          break
        case 2:
          inputArray(permissionRs, wordsArray[0])
          inputArray(permissionRs, word)
          break
        default:
          inputArray(permissionRs, wordsArray[0])
          inputArray(permissionRs, wordsArray[0] + '-' + wordsArray[1])
          inputArray(permissionRs, wordsArray[0] + '-' + wordsArray[1] +  '-' + wordsArray[2])
      }
    })
  })

  return {
    permissionRs,
    moduleNames: keys
  }
}

/**
 * 处理模块类型
 * @param array
 * @param moduleNames
 * @returns {[]}
 */
const handelModuleTypes = (array, moduleNames) => {
  const arRes = []
  array.forEach((item) => {
    if (moduleNames.includes(item.name)) {
      arRes.push(item)
    }
  })
  return arRes
}

/**
 * 处理模块信息
 * @param moduleInfos
 * @param permission
 * @returns {{moduleInfos: ({}|{}), moduleNames: []}}
 */
const handelModuleInfos = (moduleInfos, permission) => {
  const moduleInfo = {}
  const buildModuleChild = (array) => {
    const res = []
    array.forEach((item) => {
      if (permission.permissionRs.includes(item.name)) {
        const child = {}
        child.redirect = item.redirect
        child.name = item.name
        child.path = item.path
        child.meta = item.meta
        if (item.children) {
          child.children = buildModuleChild(item.children)
        }
        res.push(child)
      }
    })

    return res
  }
  permission.moduleNames.forEach((item) => {
    const module = {}
    const m = moduleInfos[item]
    module.redirect = m.redirect
    module.name = m.name
    module.path = m.path
    module.meta = m.meta
    if (m.children) {
      module.children = buildModuleChild(m.children)
    }
    moduleInfo[item] = module
  })
  return {
    moduleInfos: moduleInfo,
    moduleNames: permission.moduleNames
  }
}


export default {
  /**
   * 设置国际化选择器的值
   * @param state vuex中的值对象
   * @param val 选中的值
   */
  SET_LANGUAGE (state, val) {
    state.locale = val
  },
  /**
   * 设置路由白名单和缓存组件
   * @param state
   * @param val 路由信息val1,默认配置的白名单val2
   * @constructor
   */
  SET_WHITELIST_AND_CACHEVIEWS (state, val) {
    const whiteList = []
    const whiteListAll = []
    const cacheViews = []
    val.val2.forEach((item) => {
      if (item.endsWith('/*')) {
        whiteListAll.push(item.substring(0, item.length - 2))
      } else {
        whiteList.push(item)
      }
    })
    const routerFilter = (val) => {
      val.forEach((item) => {
        // 路由白名单添加
        if (item.meta !== undefined && item.meta.permission === false) {
          whiteList.push(item.path)
        }
        // 缓存路由添加
        if (item.meta !== undefined && item.meta.keepAlive) {
          cacheViews.push(item.name)
        }
        // 递归
        if (item.children) {
          routerFilter(item.children)
        }
      })
    }

    routerFilter(val.val1)
    state.whiteList = {
      common: whiteList,
      all: whiteListAll
    }
    state.cacheViews = cacheViews
  },
  /**
   * 设置模块信息
   *  以模块name做属性名，值为模块的路由信息
   * @param state
   * @param val
   * @constructor
   */
  SET_MODULEINFOS (state, val) {
    const moduleNames = []
    const buidModuleInfo = (val) => {
      const module = {}
      val.forEach((item) => {
        // 第一层项目层
        if (item.name) {
          moduleNames.push(item.name)
          module[item.name] = item
        }
      })
      return module
    }
    state.moduleInfos = {
      moduleNames: moduleNames,
      moduleInfos: buidModuleInfo(val)
    }
  },
  /**
   * 设置模块分类
   * @param state
   * @param val
   * @constructor
   */
  SET_MODULETYPES (state, val) {
    const basicApps = []
    const otherApps = []
    val.forEach((val) => {
      if (val.meta.type === 1) {
        basicApps.push({
          title: val.meta ? ( val.meta.title ? val.meta.title  : '' ) : '',
          i18n: val.meta ? ( val.meta.i18n ? val.meta.i18n : false ) : false,
          path: val.path,
          name: val.name,
          icon: val.meta ? ( val.meta.icon ? val.meta.icon  : '' ) : ''
        })
      }
      if (val.meta.type === 2) {
        otherApps.push({
          title: val.meta ? ( val.meta.title ? val.meta.title  : '' ) : '',
          i18n: val.meta ? ( val.meta.i18n ? val.meta.i18n : false ) : false,
          path: val.path,
          name: val.name,
          icon: val.meta ? ( val.meta.icon ? val.meta.icon  : '' ) : ''
        })
      }
    })
    state.moduleTypes = {
      basicApps,
      otherApps
    }
  },
  /**
   * 设置不做处理的模块路由信息
   * @param state
   * @param val
   * @constructor
   */
  SET_MODULEROUTES (state, val) {
    state.moduleRoutes = val
  },
  /**
   * 重置模块路由信息
   * @param state
   * @param val
   * @constructor
   */
  RESET_MODULEROUTES (state, val) {
    this.commit('SET_MODULEINFOS', state.moduleRoutes)
    this.commit('SET_MODULETYPES', state.moduleRoutes)
  },
  /**
   * 设置模块图标
   * @param state
   * @param val
   */
  SET_MODULESVG (state, val) {
    state.moduleSvg = val
  },
  /**
   * 设置默认路由
   * @param state
   * @param val
   * @constructor
   */
  SET_PERMISSIONROUTES (state, val) {
    const perHandle = handelPer(val.userInfo.permission)

    // 修改拥有的模块类型
    state.moduleTypes = {
      basicApps: handelModuleTypes(state.moduleTypes.basicApps, perHandle.moduleNames),
      otherApps: handelModuleTypes(state.moduleTypes.otherApps, perHandle.moduleNames)
    }
    // 修改模块信息
    state.moduleInfos = handelModuleInfos(state.moduleInfos.moduleInfos, perHandle)

    const defaultRoutes = []
    val.routes.forEach((item) => {
      defaultRoutes.push(item.name)
    })
    state.permissionRoutes = {
      defaultRoutes,
      permissionRoutes: perHandle.permissionRs
    }

  }
}
